/**
 * Created by chen on 2018/12/22.
 */
//调用ocx接口方法要传递的参数变量
var localIp, sipIp, sipPort, httpIp, httpPort, userCode, userName, pswd;

//设置调用ocx接口方法要传递的参数变量值
function setConfigInfo(infoArray) {
    this.sipIp = infoArray[2];
    this.sipPort = infoArray[3];
    this.httpIp = infoArray[0];
    this.httpPort = infoArray[1];
    this.userCode = infoArray[4];
    this.userName = infoArray[5];
    this.pswd = infoArray[6];
    this.localIp = infoArray[7];
}

//页面加载时调用ocxHtmlLoad方法
//if (window.attachEvent) {
//    window.attachEvent("onload", ocxHtmlLoad);
//} else if (window.addEventListener) {
//    window.addEventListener("load", ocxHtmlLoad, false);
//}
//页面改变大小时调用ocxHtmlResize方法
//    if (window.attachEvent) {
//        window.attachEvent("onresize", ocxHtmlResize);
//    } else if (window.addEventListener) {
//        window.addEventListener("resize", ocxHtmlResize, false);
//    }
//    if (window.attachEvent) {
//        window.attachEvent("onbeforeunload", uninit); //页面在反加载之前调用uninit方法
//    } else if (window.addEventListener) {
//        window.attachEvent("onbeforeunload", uninit);
//    }

var localIPArray;
var videoMainWndIndex, historyMainWndIndex;
function ocxHtmlLoad(objectId) {
    try {
        var result;
        //httpIp： httpIP地址
        //httpPort：httpIP端口
        //sipIp：   sipIP地址
        //sipPort： sip端口
        //userCode： 用户编码
        //userName： 用户名称
        //pswd：用户密码（Md5秘文）
        //localIp：本地ip地址
        var infoArray = ["10.1.150.251", 7506, "10.1.150.251", 7501, "990010000004000001", "role_ywy", "fca812f055d5fdcd3a355b63ceaad991", "10.1.166.216"];
        setConfigInfo(infoArray);
        result = document.getElementById(objectId).ocxInitLocalAddr(localIp, 0, localIp, 0);
        console.log(result);
        if (result == 0) {
            //alert('加载成功')
        } else {
            alert("视频播放控件初始化错误！");
            return;
        }
    } catch (err) {
    }
    try {
        //请求Flex执行OCX端发起执行OCX登录
        loginServer(localIp, sipIp, sipPort, httpIp, httpPort, userCode, userName, pswd ,objectId);
        //            判断控件是否加载成功
        var isResult = document.getElementById(objectId).ocxOcxIsInit();
        console.log(isResult);
        if (isResult) {
                //alert('控件加载成功');

//                document.getElementById('playerMap').ocxHVWndNums(videoMainWndIndex, 1, 1, 1);
            //创建窗口(历史视频)
            //historyMainWndIndex = document.getElementById('playerMap').ocxCreateWindow(16, 1, 0);

            //var province = document.getElementById('playerMap').ocxVideoResQueryGbk('990010000000000000');

            //if (province) setOptions(province, 'province')
//                $('#dd').text(province);
//            init();
        } else {
            alert("ocx初始化未成功！");
        }
    } catch (err) {
        txt = "视频播放控件初始化错误，无法自动下载安装控件.\n\n如果本站点不是可信任站点，请先配置为可信任站点，再重新登陆系统.";
        alert(txt);
        return;
    }
}

// 创建主窗口

function createWindow(objectId) {
    //创建窗口(实时视频)
    videoMainWndIndex = document.getElementById(objectId).ocxCreateWindow(1, 1, 1);
    // 不展示标题栏
    document.getElementById(objectId).ocxSetWndTitleBar(0);

    return videoMainWndIndex;
}

function ocxHtmlResize() {
    iBody_onresizestart(this);
}

//创建窗口
function oCreateWindow(sMaxWinNums, sDefaultWinNums) {
    playerMap.ocxCreateWindow(sMaxWinNums, sDefaultWinNums);
}
//var videoMainWndIndex;
//// 创建主窗口
//function createWindow(eleId) {
//    //创建窗口(实时视频)
//    videoMainWndIndex = document.getElementById(eleId).ocxCreateWindow(1, 1, 1);
//    // 不展示标题栏
//    document.getElementById(eleId).ocxSetWndTitleBar(0);
//
//    return videoMainWndIndex;
//}



//新的登录视频服务器 初始化方式
function loginServer(localIp, sipIp, sipPort, httpIp, httpPort,
                     userCode, userName, pswd, objectId) {
    //alert("localIp + sipIp + sipPort + httpIp + httpPort + userCode + userName + pswd=" +localIp + sipIp + sipPort + httpIp + httpPort + userCode + userName + pswd);
    try {
        //初始化本地信息
        var result = document.getElementById(objectId).ocxInitLocalAddr(localIp, 0, localIp, 0);
        console.log(result);
        var loginResult = document.getElementById(objectId).ocxLogin(httpIp, httpPort, userName, pswd, 1, 0);
        console.log('loginresult' + loginResult);
        if (loginResult == 0) {
            //setTimeout(testOpenVideo, 1000)
        } else {
            //获取错误信息
            GetOCXInitErrorString(loginResult);
        }
    } catch (err) {
        txt = "ocx控件登录错误，点击“确定”继续。\n\n";
        nalert(txt);
        return;
    }
    //parent.window.closeLoading();

}
function openVideo(videoMainWndIndexNew, p_cameraCode, pNotes, objectId) {
    //startVideo([videoMainWndIndex, 0, 150.0, "040210000003020001", "海康球机1"]);
    startVideo([videoMainWndIndexNew, 0, 150.0, p_cameraCode, pNotes], objectId);
}

// 获取错误信息
function GetOCXInitErrorString(error) {
    var str = "未知错误";

    switch (error) {
        case 180:str = "密码错误";break;
        case 168:str = "权限不足";break;
        case 169:str = "用户不存在";break;
        //case 171:str = "负荷满";break;
        case 171:str = "连接超时";break;
    }
    if (error != 0) {
        alert("控件登录失败:" + str);
    }
}
//反初始化
function uninit(videoMainWndIndexNew, objectId) {
    try {
        var r2 = document.getElementById(objectId).ocxDestroyWindow(videoMainWndIndexNew);
    } catch (err) {
        return;
    }
    try {
        var r3 = document.getElementById(objectId).ocxLogout();
    } catch (err) {
        return;
    }
    try {
        var r4 = document.getElementById(objectId).ocxUnInit();
    } catch (err) {
        return;
    }
}
//设置窗口显示个数
function pchenge(args) {
    //sMainWndIndex,sWndNums
    document.getElementById('playerMap').ocxSetWndNums(args[0], args[0]);
}
function openVideo(videoMainWndIndexNew, p_cameraCode, pNotes, objectId) {
    //startVideo([videoMainWndIndex, 0, 150.0, "040210000003020001", "海康球机1"]);
    startVideo([videoMainWndIndexNew, 0, 150.0, p_cameraCode, pNotes], objectId);
}

//调阅视频
//sMainWndIndex 主窗口号
//sSubWndNo 子窗口号
//sDecoderTag 解码标签
//p_cameraCode 摄像头编码
//pNotes 摄像头名称
function startVideo(args, objectId) {
    //sMainWndIndex,sSubWndNo,sDecoderTag,p_cameraCode,pNotes
    //alert("sMainWndIndex=" + args[0] + "//sSubWndNo=" + args[1] + "//sDecoderTag=" + args[2] + "//p_cameraCode=" + args[3] + "//pNotes=" + args[4]);
    try {
        var result = document.getElementById(objectId).ocxVideoStart(
            args[0], args[1], args[2], args[3], args[4]);
    } catch (err) {
        return;
    }
}
//停止调阅视频播放
function stopVideo(args,objectId) {
    //sMainWndIndex,sSubWndNo
    document.getElementById(objectId).ocxVideoStop(args[0], args[1]);

}
// 停止调阅所有视频播放
function stopAllVideo(sMainWndIndex, objectId) {
    //sMainWndIndex,sSubWndNo
    var result = document.getElementById(objectId).ocxVideoStopAll(
        sMainWndIndex);
    //alert(result);
}

//重新调整窗口大小
function iBody_onresizestart(de,objectId) {
    var _height = de.document.body.offsetHeight;
    var _width = de.document.body.offsetWidth;
    document.getElementById(objectId).ocxSetWindowSize(0, _height,
        _width);
}
//获取当前聚焦窗口
function ocxGetActiveWndNo(v_sMainWndIndex,objectId) {
    var index = document.getElementById(objectId).ocxGetActiveWndNo(
        v_sMainWndIndex);
    return index;
}
//获取当前是否在录像
function ocxExistRecording(objectId) {
    var result = document.getElementById(objectId).ocxExistRecording();
    return result;
}
function showXXX(sMainWndIndex, sSubWndNo, pCode) {
    // this.parent.document.getElementById('index').getActiveWndCode(sMainWndIndex, sSubWndNo, pCode);
}

function getRPath() {
    var pathName = window.location.pathname;
    var webName = pathName == '' ? '' : pathName.substring(0, pathName
        .lastIndexOf('/'));
    var rootPath = webName == '' ? window.location.protocol + '//'
    + window.location.host + webName : window.location.protocol
    + '//' + window.location.host + webName + '/';
    var path = rootPath + "assets/OcxPlayer/GridControl.zip";
    window.location.href = path;

}


////    页面初始化 显示北京
//function init() {
//    var area = document.getElementById('playerMap').ocxVideoResQueryGbk('020010000000000000');
//    $(area).find("Item").each(
//        function (i) {
//            var name = $(this).attr("Name");
//            var code = $(this).attr('Code');
//            $('<p></p>').appendTo($('.table>div:nth-child(2)')).text(name).attr('code', code);
//            $('<p></p>').appendTo($('.table>div:nth-child(1)')).text('北京');
//        });
//    var voltage = document.getElementById('playerMap').ocxVideoResQueryGbk('020210000002000000');
//
//    var biandian;
////              变电 、输电
//    $(voltage).find("Item").each(
//        function () {
//            if ($(this).attr("Name") == '变电') {
//                biandian = $(this).attr('Code');
//                return;
//            }
//        });
////              获取电压等级数据
//    var KV = document.getElementById('playerMap').ocxVideoResQueryGbk(biandian);
//    $(KV).find("Item").each(
//        function (i) {
//            var name = $(this).attr("Name").replace('110kV', '');
//            var code = $(this).attr('Code');
//            $('<p></p>').appendTo($('.table>div:nth-child(4)')).text(name).attr('code', code);
//            $('<p>110kV</p><').appendTo($('.table>div:nth-child(3)'))
//
//        });
//    var instation = document.getElementById('playerMap').ocxVideoResQueryGbk('020210000002010001');
//    $(instation).find("Item").each(
//        function (i) {
//            var name = $(this).attr("Name").replace('崇文门站', '');
//            var code = $(this).attr('Code');
//            $('<p></p>').appendTo($('.table>div:nth-child(5)')).text(name).attr('code', code);
//        });
//}